// pages/commoditydetails/commoditydetails.js
const app = getApp()
var utils = require('../../utils/util.js');
import {
  Cart
} from '.././template/specMask/specMask.js';
var WxParse = require('../../wxParse/wxParse.js');
var cart = new Cart();
Page({
  data: {
      isCoupn:false,
      isHtml: false,
      ishise: false,
      coupon:"领劵",
	  specname:"请选择"
  },
  onLoad: function(options) {
    var that = this;
    that.setData({
      id: options.id,
      number: wx.getStorageSync("number"),
    })
    //获取推荐商品
    that.getfineData(options.id);
    that.personalInfo();
    that.categorylist(options.id);
    that.commentslist(options.id);
    that.coupnlistTap(options.id);
	//   console.log(specname,"ksdjflkj")
  },

  buttonOrderCalse: function (e) {
    this.setData({
      ishise: false
    })
  },
  //领取优惠券
    onCouponTap:function(){
        this.setData({
            isCoupn: true,
            isHtml: false,
        })
    },
      //领取优惠券  关闭
    onCouponClaseTap: function () {
        this.setData({
            isCoupn: false,
            isHtml: false,
        })
    },

    //参数
    onHtmlTap: function () {
        this.setData({
            isHtml: true,
            isCoupn: false
        })
    },
    //参数
    onHtmlClaseTap: function () {
        this.setData({
            isHtml: false,
            isCoupn: false
        })
    },
  //优惠卷使用
  onIndexTap:function(e){
    var that=this;
    var index=e.currentTarget.dataset.index;
    // 收取优惠券
      utils.http("/api/v1/coupon/get_coupon", {
        coupon_id: that.data.couponList[index].id
      }, function (res) {
        if (res.state == 1) {
          that.setData({
            isCoupn: false,
            coupon: that.data.couponList[index].money
          })
        } else {
          utils.message(res.datas)
        }
      })
    
  },
  saveCanvas: function() {
    var that = this;
    var url = 'https://xinong.qidian2018.com/index.php?share_id=1&type=3&id=' + that.data.data + '&member_id=' + that.data.info.id;
    console.log(url);
    that.setData({
      qrcodeValue: url,
    }, function() {
      var canvas = that.selectComponent('#goods-share');
      canvas.saveImg();
    })
  },
  //分享弹窗
  onshareTap: function (e) {
    var that = this;
    var url = 'https://xinong.qidian2018.com/index.php?share_id=1&type=3&id=' + that.data.data.id+'&member_id=' + that.data.info.id;
    that.setData({
      qrcodeValue: url,
      ishise: true
    })
    console.log(this.data.qrcodeValue)
  },
  getfineData: function(goods_id) {
    var that = this;
    utils.showLoading();
    utils.http("/api/v1/goods/recom_goods_list", {
      goods_id: goods_id
    }, function(res) {
      utils.hideLoading();
      that.setData({
        fineData: res.datas,
        // fineData: wx.getStorageSync('fineData'),
      });
    })
  },
  noFrutisDetailsTap: function(e) {
    var id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '../commoditydetails/commoditydetails?id=' + id,
    })
  },
  lll: function(e) {
    var that = this;
    if (that.data.dataDetail != undefined) {
      var dataDetail = that.data.dataDetail;
      dataDetail.number = 0;
      for (var k in wx.getStorageSync("listPost")) {
        if (wx.getStorageSync("listPost")[k].id == dataDetail.id) {
          dataDetail.number = wx.getStorageSync("listPost")[k].number;
        }
      }
      that.setData({
        dataDetail: dataDetail
      })
    }
  },
  //商品详情
  categorylist: function(id) {
    var that = this;
    utils.showLoading();
    utils.http("/api/v1/goods/detail", {
      id: id
    }, function(res) {
      utils.hideLoading();
      that.setData({
        dataDetail: res.datas,
      });
      cart.init(that, res.datas);
      wx.setStorageSync('dataDetail', res.datas);
      WxParse.wxParse('article', 'html', res.datas.content, that, 5);
		WxParse.wxParse('article1', 'html', res.datas.goods_param, that, 5);
      that.lll();
    })

  },
  //优惠券列表
  coupnlistTap: function (id){
    var that = this;
    utils.showLoading();
    utils.http("/api/v1/goods/coupon_list", {
      goods_id: id
    }, function (res) {
      utils.hideLoading();
      that.setData({
        couponList: res.datas,
      });
    })
  },
  //商品评价
  commentslist: function(id) {
    var that = this;
    utils.showLoading();
    utils.http("/api/v1/goods/comments_list", {
      be_id: id
    }, function(res) {
      utils.hideLoading();
      var commentsPost = []
      for (var i = 0; i < 2; i++) {
        commentsPost.push(res.datas.list[i])
      }
      that.setData({
        comments: commentsPost,
        commentsAll: res.datas.list
      })
    })
  },
  //添加购物车
  addCarTap: function(e) {
    var that = this;
    var shuzi = wx.getStorageSync("number") || 0;
    var number = shuzi + 1;
    that.data.dataDetail.number = that.data.dataDetail.number + 1
    that.setData({
      number: number,
      dataDetail: that.data.dataDetail
    })
    wx.setStorageSync("number", number);
    var dataInfo = {
      id: e.currentTarget.dataset.id,
      image: e.currentTarget.dataset.image,
      name: e.currentTarget.dataset.name,
      price: e.currentTarget.dataset.price,
      subtitle: e.currentTarget.dataset.subtitle,
      vip_price: e.currentTarget.dataset.vipprice,
      selectStatus: false, //购物车做删除时候用
      number: 1 //加减数量
    }
    utils.uniqueSearchRecord(dataInfo);
  },
  //浏览图片
  imgTap: function(e) {
    var that = this;
    var index = e.currentTarget.dataset.index;
    wx.previewImage({
      current: e.currentTarget.dataset.http, // 当前显示图片的http链接
      urls: that.data.comments[index].image // 需要预览的图片http链接列表
    })
  },
  //跳转商品评价更多页面
  onEvaluateTap: function(e) {
    var audioCtx = wx.createVideoContext('myAudio');
    audioCtx.pause();
    wx.navigateTo({
      url: '../commoditydetails/moreevaluatelist/moreevaluatelist?id=' + this.data.id,
    })
  },
  //立即购买
  buyNowTap: function(e) {
    var that = this;
    var shuzi = wx.getStorageSync("number") || 0;
    var number = shuzi + 1;
    that.data.dataDetail.number = that.data.dataDetail.number + 1
    wx.setStorageSync("number", number);
    var dataInfo = {
      id: e.currentTarget.dataset.id,
      image: e.currentTarget.dataset.image,
      name: e.currentTarget.dataset.name,
      price: e.currentTarget.dataset.price,
      subtitle: e.currentTarget.dataset.subtitle,
      vip_price: e.currentTarget.dataset.vipprice,
      selectStatus: true, //购物车做删除时候用
      number: 1 //加减数量
    }
    utils.uniqueSearchRecord(dataInfo);
    //判断如果当前商品从详情进入购物车，购物车只显示当前商品选中状态 
    var listPost = wx.getStorageSync("listPost")
    for (var i in listPost) {
      if (listPost[i].id != dataInfo.id) {
        listPost[i].selectStatus = false;
      }
      wx.setStorageSync("listPost", listPost)
    }
    wx.switchTab({
      url: '../cartshopp/cartshopp',
    })
  },
  //我的个人信息
  personalInfo: function(e) {
    var that = this;
    utils.http("/api/v1/member/index", {}, function(res) {
      that.setData({
        info: res.datas,
      })

    })

  },
  //判断是不是当前视频播放状态
  bindtransitionTap: function(e) {
    var audioCtx = wx.createVideoContext('myAudio');
    console.log(audioCtx)
    if (e.detail.current != 0) {
      audioCtx.pause()
    } else {
      audioCtx.play()
    }
  },



  // 点击cover播放，其它视频结束 
  videoPlay: function(e) {
    var audioCtx = wx.createVideoContext('myAudio');
    audioCtx.pause();
    var _index = e.currentTarget.id
    console.log(_index)
    this.setData({
      _index: _index
    })
    //停止正在播放的视频 
    var videoContextPrev = wx.createVideoContext(this.data._index)
    videoContextPrev.stop();
    setTimeout(function() { //将点击视频进行播放   
      var videoContext = wx.createVideoContext(_index)
      videoContext.play();
    }, 500)
  },
  showMask: function() {
    cart.showMask();
  },
  closeMask: function() {
    cart.closeMask();
  },
  chooseSpec: function(e) {
    cart.chooseSpec(e);
  },
  nowBuy: function() {
    cart.nowBuy();
  },
  addCart: function() {
    cart.addCart();
  },
  add: function() {
    cart.add();
  },
  less: function() {
    cart.less();
  },
  onShareAppMessage: function() {
	  return utils.share("", this.data.dataDetail.name);
  }
})